using System;
using Microsoft.WowAddonStudio.FrameXml.Components.Descriptors;
using Microsoft.WowAddonStudio.FrameXml.Components.Events.ScriptManager;
using Microsoft.WowAddonStudio.FrameXml.Components.Tests.Mocks;
using Microsoft.WowAddonStudio.FrameXml.Serialization;
using Microsoft.WowAddonStudio.Tests.Common.Mocks;

using NUnit.Framework;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Tests.FrameXmlEvents
{
    [TestFixture]
    public class FrameXmlPropertyDescriptorTest
    {
        [SetUp]
        public void SetUp()
        {
        }

        [TearDown]
        public void TearDown()
        {
        }

        /// <summary>
        /// Adds the script.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="eventName">Name of the event.</param>
        /// <param name="script">The script.</param>
        private static void AddScript(FrameControl control, EventName eventName, string script)
        {
            control.ScriptContainer.Add(eventName, script);
        }

        [Test(Description = "")]
        public void Test_Init()
        {
            var eventDescriptor = new FrameXmlEventDescriptor(typeof(string), EventName.OnAnimFinished);
            var propertyDescriptor = new FrameXmlEventPropertyDescriptor(eventDescriptor);

            Assert.AreEqual(typeof(string), eventDescriptor.ComponentType);
            Assert.AreSame(eventDescriptor, propertyDescriptor.Event);
            Assert.AreEqual(typeof(string), propertyDescriptor.PropertyType);
            Assert.IsFalse(propertyDescriptor.IsReadOnly);
            Assert.IsNull(propertyDescriptor.ScriptBlock);
        }

        [Test(Description = "")]
        public void Test_Operations()
        {
            const string testScript = "print(10);";
            var serviceLocator = new SimpleTestServiceLocator();
            serviceLocator.AddService(typeof(IFrameXmlScriptManager), new MockFrameXmlScriptManager());
            var eventDescriptor = new FrameXmlEventDescriptor(typeof(string), EventName.OnAnimFinished);
            var propertyDescriptor = new FrameXmlEventPropertyDescriptor(eventDescriptor);

            var control = new ButtonControl { Site = serviceLocator };
            AddScript(control, EventName.OnChar, testScript);

            object value = propertyDescriptor.GetValue(control);
            Assert.IsNull(value);

            AddScript(control, EventName.OnAnimFinished, testScript);
            value = propertyDescriptor.GetValue(control);
            Assert.IsNotNull(value);
            Console.WriteLine(value);
            Assert.AreEqual(testScript, value);

            value = propertyDescriptor.GetValue(new object());
            Assert.IsNull(value);

            propertyDescriptor.SetValue(control, "StartTest();");

            value = propertyDescriptor.GetValue(control);
            Assert.IsNotNull(value);
            Console.WriteLine(value);
            Assert.AreEqual("StartTest();", value);

            propertyDescriptor.ResetValue(control);
            value = propertyDescriptor.GetValue(control);
            Assert.IsNull(value);
        }
    }
}